const app = getApp();
var util = require('../../utils/util.js');
Page({
  data: {
    region: ['辽宁省', '沈阳市', '和平区'],
    iconList: [{
      icon: 'tag',
      color: 'orange',
      badge: 0,
      name: "打药服务",
      url:'firstpage'
    }, {
      icon: 'deliver',
      color: 'cyan',
      badge: 0,
        name: "全年托管",
        url: ''
    }, {
      icon: 'goodsnew',
      color: 'olive',
      badge: 22,
        name: "作物诊断",
        url: ''
    }, {
      icon: 'cascades',
      color: 'blue',
      badge: 0,
        name: "更多",
        url: ''
    }, ],
    gridCol: 4,
    skin: false,
    bindPickerChange: 0,
    showImage: true,
    /**药剂类型DATA */
    t_index: 0,

    /**
     * pesticide农药类型选项;pesticideValue对应的农药价格
     */
    pesticide: ["", "先正达", "富美实", "诺普信"],
    pesticideValue: ["0", "20", "30", "40"],
    pesticideIndex: 1,
    supplyFlag: false,
    navTitle: ["服务", "发现", "商城"],
    pageTitle: ["服务", "发现", "商城"],
    TabCur: 0,
    scrollLeft: 0,
    icon: [{
      name: 'my',
      isShow: true
    }, {
      name: 'mark',
      isShow: true
    }],
    radioItems: [{
        name: '0',
        value: '平台提供',
        checked: 'true'
      },
      {
        name: '1',
        value: '自己带药'
      },
    ],

  },
  //隐藏图片
  hideModal(e) {
    //console.log(e);
    this.setData({
      showImage: false
    })
  },
  //导航栏
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id, //获取用户点击的ID
    })
  },

  onLoad: function() {
    var DATE = util.formatTime(new Date());
    this.setData({
      date: DATE,
    })
  },

  //带药方式
  bindPickerChange(e) {
    console.log('picker发送选择改变，携带值为【带药方式】：', e.detail.value)
    if (e.detail.value == 1) {
      this.setData({
        bindPickerChange: 1,
        supplyFlag: true,

      })
    } else {
      this.setData({
        bindPickerChange: e.detail.value,
        supplyFlag: false,

      })
    }
  },
  //药剂类型
  chooseTypeOfPesticide: function(e) {
    console.log(' picker发生选择改变，携带值为【农药类型】：', e.detail.value);
    this.setData({
      pesticideIndex: e.detail.value
    })
  },
  //区域
  bindRegionChange: function(e) {
    console.log('picker发送选择改变，携带值为【选择区域】：', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  //详细地址
  detailAddressInput: function(e) {
    this.setData({
      detailAddress: e.detail.value
    })
  },
  //喷洒时间
  bindDateChange(e) {
    console.log('picker发送选择改变，携带值为【喷洒时间】：', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  //亩数
  areaRegion: function(e) {
    this.setData({
      areaRegion: e.detail.value
    })
  },
  //联系人姓名
  contactNameInput: function(e) {
    this.setData({
      contactName: e.detail.value
    })
  },
  //联系电话
  telNumberInput: function(e) {
    this.setData({
      telNumber: e.detail.value
    })
  },
  //我要发布需求
  SubmitBtnClick: function(e) {
    //console.log(this.data)
    const db = wx.cloud.database()

    if (this.data.contactName == null || this.data.contactName == '' ||
      this.data.telNumber == null || this.data.telNumber == '' ||
      this.data.detailAddress == null || this.data.detailAddress == ''
      /*|| this.data.areaRegion == null || this.data.areaRegion == ''
      || this.data.date == null || this.data.date == ''
      || this.data.pesticideIndex == null || this.data.pesticideIndex == ''*/
    ) {
      wx.showToast({
        icon: 'none',
        title: '订单信息不完整，请检查后再提交',
      })
    } else {
      db.collection('Order').add({
        data: {
          contactName: this.data.contactName, //联系人姓名
          telNumber: this.data.telNumber, //电话号码
          region: this.data.region, //地址
          region1: this.data.region[1], //区域
          detailAddress: this.data.detailAddress, //详细地址
          areaRegion: this.data.areaRegion, //面积
          date: this.data.date, //打药日期
          medicineWay: this.data.bindPickerChange, //带药方式
          pesticideType: this.data.pesticideIndex, //农药类型
          orderState: '0', //订单状态
          feedback: '' //我的评价

        },
        success: res => {
          // console.log(res);
          // 在返回结果中会包含新创建的记录的 _id
          this.setData({
            counterId: res._id,
          })
          // wx.showToast({
          //   title: '提交订单成功',
          // })
          wx.navigateTo({
            url: 'makeOrder_success/makeOrder_success?_id=' + res._id
          })
          console.log('[数据库] [新增记录] 成功，记录 _id: ', res._id)

        },
        fail: err => {
          wx.showToast({
            icon: 'none',
            title: '提交订单失败'
          })

          console.error('[数据库] [新增记录] 失败：', err)

        }
      })

      // console.log("联系人：" + this.data.contactName +
      //   "手机号码：" + this.data.telNumber +
      //   "地址：" + this.data.region +
      //   "详细地址：" + this.data.detailAddress +
      //   "喷洒面积：" + this.data.areaRegion +
      //   "喷洒时间：" + this.data.date +
      //   "带药方式：" + this.data.bindPickerChange +
      //   "农药类型：" + this.data.pesticideIndex);
    }
  },
})